// <copyright file="GraphicsDevice.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>April 2009</date>
// <summary>Double buffered screen graphics manager.</summary>

// License:

// Product: Ascii / GDI+ Graphics Manager. Library for RPGRoguelikeEngine .Net.
// Author: Robert N. Tenney
// Copyright: April 2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System.Drawing;
using System.Drawing.Drawing2D;

namespace TenneySoftware.GraphicsManagers {
    /// <summary>
    /// This is the class the defines and handles the screen graphics for double buffering.
    /// </summary>
    public class GraphicsDevice {
        #region Fields:
        /// <summary>
        /// Context to use for the buffered graphics.
        /// </summary>
        private BufferedGraphicsContext context;

        /// <summary>
        /// Backbuffer to draw to.
        /// </summary>
        private BufferedGraphics buffer;

        /// <summary>
        /// Background color for screen clearing.
        /// </summary>
        private Color backgroundColor;
        #endregion

        #region Constructors:
        /// <summary>
        /// Initializes a new instance of the GraphicsDevice class.
        /// </summary>
        /// <param name="control">Control to draw onto.</param>
        public GraphicsDevice(System.Windows.Forms.Control control) {
            this.context = BufferedGraphicsManager.Current;
            this.buffer = this.context.Allocate(control.CreateGraphics(), control.DisplayRectangle);
            this.buffer.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
            this.backgroundColor = Color.CornflowerBlue;
        }
        #endregion

        #region Destructors:
        /// <summary>
        /// Finalizes an instance of the GraphicsDevice class.
        /// </summary>
        ~GraphicsDevice() {
            this.buffer.Dispose();
        }
        #endregion

        #region Properties:
        /// <summary>
        /// Gets the buffer to use for the double buffering.
        /// </summary>
        /// <value>
        /// The buffer to use for the double buffering.
        /// </value>
        public BufferedGraphics DeviceBuffer {
            get {
                return this.buffer;
            }
        }

        /// <summary>
        /// Gets or sets the background color for the drawing surface.
        /// </summary>
        /// <value>
        /// The background color for the drawing surface.
        /// </value>
        public Color BackgroundColor {
            get {
                return this.backgroundColor;
            }

            set {
                this.backgroundColor = value;
            }
        }
        #endregion

        #region Public Methods:
        /// <summary>
        /// Defines the method of drawing the objects onto screen.
        /// This is set to default, unless changed by user.
        /// </summary>
        /// <param name="mode">Mode to set.</param>
        public void SmoothMode(SmoothingMode mode) {
            this.buffer.Graphics.SmoothingMode = mode;
        }

        /// <summary>
        /// Method to draw the buffer to the form.
        /// </summary>
        public void Draw() {
            this.buffer.Render();
        }

        /// <summary>
        /// Method to clear the background with the defined background color.
        /// </summary>
        public void ClearBackground() {
            this.buffer.Graphics.Clear(this.backgroundColor);
        }
        #endregion
    }
}
